<template>
  <div class="mobile-nav-container">
    <!-- 添加占位 div -->
    <div class="header-placeholder"></div>

    <header class="mobile-header">
      <div class="nav-belt">
        <!-- 左侧区域 -->
        <div class="nav-left">
          <!-- 菜单按钮 -->
          <button class="menu-btn" @click="openSideNav">
            <i class="fas fa-bars"></i>
          </button>

          <!-- Logo -->
          <router-link to="/" class="nav-logo">
            <img src="@/assets/images/Amazon_logo.svg" alt="Amazon" />
          </router-link>
        </div>

        <!-- 右侧区域 -->
        <div class="nav-right">
          <!-- 登录/我的账户按钮 -->
          <router-link
            :to="userStore.isLogin ? '/account' : '/login'"
            class="nav-item account-btn"
          >
            <div class="account-text">
              <span>{{ userStore.isLogin ? userStore.userInfo?.member?.nickName : $t('请登录') }}</span>
              <i class="fas fa-angle-right"></i>
            </div>
            <div class="account-icon">
              <img src="@/assets/images/account/account-wave2x.gif" alt="user" />
            </div>
          </router-link>

          <!-- 购物车按钮 -->
          <router-link
            to="/products/cart"
            class="cart-link"
          >
            <div class="nav-cart-wrapper">
              <div class="nav-cart-icon nav-sprite"></div>
              <span class="nav-cart-count">{{ cartStore.shopCartTotal }}</span>
            </div>
            <span class="cart-text">{{ $t('购物车') }}</span>
          </router-link>
        </div>
      </div>

      <!-- 搜索栏 -->
      <div class="nav-search">
        <div class="nav-search-wrapper">
          <input
            type="text"
            class="nav-search-input"
            :placeholder="$t('搜索 Amazon 优惠券')"
          >
          <button class="nav-search-submit">
            <i class="fas fa-search"></i>
          </button>
        </div>
      </div>
    </header>
    <MobileSideNav ref="sideNavRef" />
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import useUserStore from '@/store/modules/user'
import useCartStore from '@/store/modules/cart'
import {ref} from "vue";
import MobileSideNav from "@/components/MobileSideNav.vue";

const router = useRouter()
const userStore = useUserStore()
const cartStore = useCartStore()

const sideNavRef = ref()


const openSideNav = () => {
  if (sideNavRef.value) {
    sideNavRef.value.open()
  }
}
</script>

<style lang="scss" scoped>
.mobile-nav-container {
  position: relative;
  width: 100%;
}

.header-placeholder {
  display: none;
}

.mobile-header {
  width: 100%;
  z-index: 1000;
  background: #222f3e;

  .nav-belt {
    height: 55px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #222f3e;
  }

  .nav-left {
    display: flex;
    align-items: center;
    gap: 4px;
    padding-left: 8px;

    .menu-btn {
      width: 40px;
      height: 40px;
      margin-left: -8px;
      background: none;
      border: none;
      color: $text-white;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;

      i {
        font-size: 22px;
      }
    }

    .nav-logo {
      height: 28px;

      img {
        height: 100%;
        width: auto;
      }
    }
  }

  .nav-right {
    display: flex;
    align-items: center;
    gap: 0;
    margin-left: auto;

    .nav-item {
      height: 40px;
      padding: 0;
      color: $text-white;
      text-decoration: none;
      display: flex;
      align-items: center;
    }

    .account-btn {
      display: flex;
      align-items: center;
      gap: 6px;
      flex-direction: row;

      .account-text {
        display: flex;
        align-items: center;
        gap: 4px;
        font-size: 13px;
        color: $text-white;
        order: -1;

        .fa-angle-right {
          font-size: 12px;
          margin-top: 1px;
        }
      }
    }

    .account-icon {
      position: relative;

      img {
        width: 35px;
        height: 27px;
        object-fit: contain;
        vertical-align: middle;
      }
    }

    .cart-link {
      display: flex;
      align-items: center;
      gap: 4px;
      color: $text-white;
      text-decoration: none;
      height: 40px;
      padding: 0 8px;

      .nav-cart-wrapper {
        position: relative;
        display: flex;
        align-items: center;

        .nav-cart-icon {
          width: 38px;
          height: 26px;
          background-position: -10px -340px;
        }
      }

      .nav-cart-count {
        position: absolute;
        top: -4px;
        left: 13px;
        min-width: 19px;
        height: 19px;
        color: $secondary-color;
        font-size: 16px;
        line-height: 19px;
        text-align: center;
        font-weight: bold;
      }

      .cart-text {
        font-size: 13px;
        display: none;
      }
    }
  }

  .nav-search {
    background: #222f3e;
    padding: 0 8px 8px;

    &-wrapper {
      display: flex;
      height: 38px;
      background: $text-white;
      border-radius: 4px;
      overflow: hidden;
      max-width: 100%;
    }

    &-input {
      flex: 1;
      height: 100%;
      padding: 0 12px;
      border: none;
      outline: none;
      font-size: 14px;
      width: 0;
      min-width: 0;
    }

    &-submit {
      flex-shrink: 0;
      width: 42px;
      height: 100%;
      border: none;
      background: $hover-color;
      color: $text-primary;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;

      i {
        font-size: 18px;
      }
    }
  }
}

.nav-sprite {
  background-image: url(@/assets/images/nav-sprite-global-1x-reorg-privacy._CB541706272_.png);
  background-repeat: no-repeat;
  display: inline-block;
}

// 添加全局样式，确保页面内容不被遮挡
:deep(body) {
  padding-top: 101px; // nav-belt(55px) + search-bar(38px) + padding(8px)
}
</style>
